package com.paipaideli.common.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2019/9/29.
 */

public class BitmapUtil {
    private BitmapUtil() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    public static int getBitmapDegree(String path) {
        short degree = 0;

        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt("Orientation", 1);
            switch(orientation) {
                case 3:
                    degree = 180;
                    break;
                case 6:
                    degree = 90;
                    break;
                case 8:
                    degree = 270;
            }
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return degree;
    }

    public static Bitmap rotateBitmapByDegree(Bitmap bitmap, int degree) {
        Matrix matrix = new Matrix();
        matrix.postRotate((float)degree);
        Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        if(!bitmap.isRecycled()) {
            bitmap.recycle();
        }

        return newBitmap;
    }

    public static Uri getRotatedUri(Activity activity, String path) {
        int degree = getBitmapDegree(path);
        if(degree != 0) {
            Bitmap bitmap = BitmapFactory.decodeFile(path);
            Bitmap newBitmap = rotateBitmapByDegree(bitmap, degree);
            return Uri.parse(MediaStore.Images.Media.insertImage(activity.getContentResolver(), newBitmap, (String)null, (String)null));
        } else {
            return Uri.fromFile(new File(path));
        }
    }

    public static Bitmap rotateBitmapByDegree(String path, int degree) {
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        return rotateBitmapByDegree(bitmap, degree);
    }

    public static void compressAndGenImage(String imgPath, String outPath, int maxSize) throws IOException {
        if(imgPath != null && !TextUtils.isEmpty(imgPath)) {
            Bitmap image = revitionImageSize(imgPath);
            if(image != null) {
                int degree = getBitmapDegree(imgPath);
                if(degree != 0 && image != null) {
                    image = rotateBitmapByDegree(image, degree);
                }

                ByteArrayOutputStream os = new ByteArrayOutputStream();
                int options = 100;
                image.compress(Bitmap.CompressFormat.JPEG, options, os);
                int var7 = os.toByteArray().length;

                while(os.toByteArray().length / 1024 > maxSize) {
                    os.reset();
                    options -= 10;
                    image.compress(Bitmap.CompressFormat.JPEG, options, os);
                }

                FileOutputStream fos = new FileOutputStream(outPath);
                fos.write(os.toByteArray());
                fos.flush();
                fos.close();
            }
        }
    }

    public static Bitmap revitionImageSize(String path) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, (Rect)null, options);
        in.close();
        int i = 0;

        Bitmap bitmap;
        for(bitmap = null; options.outWidth >> i > 1500 || options.outHeight >> i > 1500; ++i) {
            ;
        }

        in = new BufferedInputStream(new FileInputStream(new File(path)));
        options.inSampleSize = (int)Math.pow(2.0D, (double)i);
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeStream(in, (Rect)null, options);
        return bitmap;
    }
}
